<template>
  <div class="play-content">
    <div class="top-wrap">
      <div class="img-wrap">
        <img :src="coverImgUrl" alt="" />
      </div>
      <div class="info-wrap">
        <p class="title">{{ title }}</p>
        <div class="author-wrap">
          <img class="avatar" :src="avatarUrl" alt="" />
          <span class="name">{{ nickname }}</span>
        </div>
        <div class="tag-wrap">
          <span class="title">标签:</span>
          <ul>
            <li v-for="item in tags" :key="item">{{ item }}</li>
          </ul>
        </div>
        <div class="desc-wrap">
          <span class="title">简介:</span>
          <span class="desc">{{ signature }}</span>
        </div>
      </div>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲列表" name="1">
        <table class="el-table playlit-table">
          <thead>
            <th></th>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              class="el-table__row song-table"
              v-for="(item, index) in tableData"
              :key="item.id"
            >
              <td>{{ index + 1 }}</td>
              <td>
                <div class="img-wrap" @click="playMusic(item.id,item.al.picUrl)">
                  <img :src="item.al.picUrl" alt="" />
                  <span class="iconfont icon-play el-icon-video-play"></span>
                </div>
              </td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{ item.name }}</span>
                    <!-- mv图标 -->
                    <span v-if="item.mv != 0" @click="toMV(item.mv)" class="iconfont icon-mv el-icon-view"></span>
                  </div>
                  <span>{{ item.subTitle }}</span>
                </div>
              </td>
              <td>{{ item.ar[0].name }}</td>
              <td>
                {{ item.al.name }}
              </td>
              <td>{{ item.dt | formatDuration }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane :label="`评论(${total + hotComments.length})`" name="2">
        <!-- 热门评论 -->
        <div v-if="hotComments.length != 0" class="comment-wrap">
          <p class="title">
            热门评论<span class="number">({{ hotComments.length }})</span>
          </p>
          <div
            class="comments-wrap"
            v-for="item in hotComments"
            :key="item.commentId"
          >
            <div class="item">
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}：</span>
                  <span class="comment">{{ item.content }}</span>
                </div>
                <div v-if="item.beReplied.length != 0" class="re-content">
                  <span class="name"
                    >{{ item.beReplied[0].user.nickname }}：</span
                  >
                  <span class="comment">{{ item.beReplied[0].content }}</span>
                </div>
                <div class="date">{{ item.time | formatTime }}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 最新评论 -->
        <div class="comment-wrap">
          <p class="title">
            最新评论<span class="number">({{ total }})</span>
          </p>
          <div v-if="comments.length != 0" class="comments-wrap">
            <div class="item" v-for="item in comments" :key="item.commentId">
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}：</span>
                  <span class="comment">{{ item.content }}</span>
                </div>
                <div v-if="item.beReplied.length != 0" class="re-content">
                  <span class="name"
                    >{{ item.beReplied[0].user.nickname }}：</span
                  >
                  <span class="comment">{{ item.beReplied[0].content }}</span>
                </div>
                <div class="date">{{ item.time | formatTime }}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 分页器 -->
        <el-pagination
          @current-change="handleCurrentChange"
          background
          small
          layout="prev, pager, next"
          :total="total"
          :current-page="page"
          :page-size="limit"
        >
        </el-pagination>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { playlistDetail, listComments, hotComments } from '@/api/playlist'
import { songUrl } from '@/api/home'
import bus from '@/utils/bus'
export default {
  name: 'PlayIndex',
  components: {},
  props: {},
  data () {
    return {
      activeIndex: '1',
      tableData: [],
      title: '',
      avatarUrl: '',
      coverImgUrl: '',
      signature: '',
      nickname: '',
      tags: [],
      // 热门评论
      hotComments: [],
      // 普通评论
      comments: [],
      // 分页相关数据
      // 每页数据
      limit: 10,
      // 页码
      page: 1,
      // 总条数
      total: 0
    }
  },
  computed: {},
  watch: {},
  created () {
    const { id } = this.$route.params
    playlistDetail(id).then(res => {
      // 歌曲信息
      this.tableData = res.data.playlist.tracks
      this.title = res.data.playlist.name
      this.avatarUrl = res.data.playlist.creator.avatarUrl
      this.coverImgUrl = res.data.playlist.coverImgUrl
      this.signature = res.data.playlist.creator.signature
      this.nickname = res.data.playlist.creator.nickname
      this.tags = res.data.playlist.tags
    })
    // 获取热门评论
    hotComments(id).then(res => {
      this.hotComments = res.data.hotComments
    })

    // 获取评论信息
    this.getComments()
  },
  mounted () {},
  methods: {
    toMV (id) {
      this.$router.push({
        name: 'm.mvlist',
        params: {
          id
        }
      })
    },
    // 获取歌曲评论
    getComments () {
      const { id } = this.$route.params
      // 获取歌曲评论
      listComments(
        id,
        this.limit,
        (this.page - 1) * this.limit
      ).then(res => {
        this.total = res.data.total
        this.comments = res.data.comments
      })
    },
    // 页码改变
    handleCurrentChange (val) {
      this.page = val
      this.getComments()
    },
    async playMusic (id, img) {
      // const res = await songUrl(id)
      bus.$emit('songId', id, img)
    }
  }
}
</script>

<style scoped lang="less">
.play-content {
  margin: auto;
  .top-wrap {
    display: flex;
    span {
      font-size: 14px;
    }
    .img-wrap {
      margin-right: 20px;
      img {
        width: 100px;
        height: 100px;
      }
    }
    .info-wrap {
      .title {
        margin: 10px 0;
        font-size: 12px;
      }
      .author-wrap {
        display: flex;
        align-items: center;
        margin-bottom: 5px;
        img {
          width: 30px;
          height: 30px;
          border-radius: 50%;
          margin-right: 10px;
        }
        .name {
          margin-right: 10px;
        }
        .time {
          font-size: 12px;
        }
      }
      .play-wrap {
        width: 120px;
        height: 35px;
        border-radius: 4px;
        background: linear-gradient(to right, #e85e4d, #c6483c);
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 5px;
        span {
          color: white;
          &.iconfont {
            margin-right: 8px;
          }
          &.text {
            font-size: 14px;
          }
        }
      }
      .tag-wrap {
        display: flex;
        margin-bottom: 5px;
        span {
          margin: 0;
          text-align: center;
        }
        ul {
          display: flex;
          align-items: center;
        }
        li {
          font-size: 12px;
          text-align: center;
          &:not(:last-child)::after {
            content: '/';
            margin: 0 4px;
          }
        }
      }
      .desc-wrap {
        span {
          &:last-child {
            font-size: 12px;
          }
        }
      }
      span:first-child {
        margin-right: 10px;
      }
    }
  }
  .song-table {
    .song-wrap {
      > span {
        margin-top: 8px;
        display: inline-block;
        color: #bebebe;
        overflow:hidden;
        text-overflow:ellipsis;
        white-space:nowrap;
      }
      .icon-mv {
        padding-left: 5px;
        color: #dd6d60;
      }
    }
    .img-wrap {
      position: relative;
      width: 36px;
      height: 36px;
      img {
        width: 36px;
        height: 36px;
        border-radius: 5px;
      }
      .icon-play {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 12px;
        height: 12px;
        color: #dd6d60;
        font-size: 12px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(255, 255, 255, 0.8);
      }
    }
  }
  .comment-wrap {
    .title {
      font-size: 14px;
      .number {
        color: black;
        font-size: 14px;
      }
    }
    .item {
      display: flex;
      padding-top: 20px;
      .icon-wrap {
        margin-right: 15px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
        }
      }
      &:not(:last-child) {
        .content-wrap {
          border-bottom: 1px solid #f2f2f1;
        }
      }
      .date {
        color: #bebebe;
        font-size: 12px;
      }

      .content-wrap {
        padding-bottom: 20px;
        .name {
          color: #517eaf;
          font-size: 12px;
        }
        .comment {
          font-size: 12px;
        }
        .content,
        .re-content {
          margin-bottom: 10px;
        }
        .re-content {
          padding: 10px;
          background-color: #e6e5e6;
          border-radius: 5px;
        }
      }
    }
  }
}
.el-table th, .el-table tr {
  background-color: #e9eef3;
}
.el-table, .el-table__expanded-cell{
  background-color: #e9eef3;
}
/deep/.el-tabs__header{
  margin: 0;
}
/deep/.el-table{
  margin-bottom: 24px;
  font-size: 12px;
}
/deep/.el-table::before{
  content: none;
}
/deep/.el-pagination{
  margin-bottom: 52px;
}
</style>
